package com.yk.anima.servlet;

import com.github.pagehelper.Page;
import com.yk.anima.dao.ProductsCatMapper;
import com.yk.anima.exption.ExceptionCast;
import com.yk.anima.pojo.ProductsCat;
import com.yk.anima.response.CommonCode;
import com.yk.anima.response.QueryResponseResult;
import com.yk.anima.response.QueryResult;
import com.yk.anima.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;

import java.util.List;

/**
 * @author xiaokai
 * @create 2021-05-13 18:16:43
 * @desc...猫的业务处理
 */
@Service
public class ProductsCatServlet {

    @Autowired
    ProductsCatMapper productsCatMapper;

    public List<ProductsCat> findAll() {
        List<ProductsCat> all = productsCatMapper.findAll();
        return all;
    }

    public ProductsCat findById(int id) {

        ProductsCat productsCat = productsCatMapper.selectByPrimaryKey(id);
        if (productsCat == null || "".toString().equals(productsCat)) {
            //该猫不存在
            ExceptionCast.cast(CommonCode.CATNULL);
        }
        return productsCat;
    }

    public ResponseResult save(ProductsCat productsCat) {
        if (productsCat == null) {
            productsCat = new ProductsCat();
        }
        productsCat.setpId(null);
        productsCat.setCatId(1);

        int number = productsCatMapper.insertSelective(productsCat);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);

    }

    public ResponseResult update(int id, ProductsCat productsCat) {

        ProductsCat cat = this.findById(id);
        if (productsCat == null || "".toString().equals(productsCat)) {
            ExceptionCast.cast(CommonCode.CATNULL);
        }
        //名字
        String name = productsCat.getName();
        //年龄
        String age = productsCat.getAge();
        //描述
        String description = productsCat.getDescription();
        //价钱
        String price = productsCat.getPrice();
        //性别
        String gender = productsCat.getGender();
        cat.setName(name);
        cat.setAge(age);
        cat.setDescription(description);
        cat.setPrice(price);
        cat.setGender(gender);
        cat.setCatId(1);
        cat.setpId(id);
        int number = productsCatMapper.updateByPrimarySelective(cat);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.UPDATEFAIL);
    }

    public ResponseResult deleteById(int id) {
        this.findById(id);
        int number = productsCatMapper.deleteByPrimaryKey(id);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    public QueryResponseResult findPage(int page, int size, String name) {
        if (page <= 0) {
            page = 0;
        }
        if (size <= 0) {
            size = 2;
        }

        PageHelper.startPage(page, size);
        Page<ProductsCat> catPage = productsCatMapper.findByName(name);
        List<ProductsCat> list = catPage.getResult();
        long total = catPage.getTotal();

        QueryResult<ProductsCat> queryResult = new QueryResult<ProductsCat>();

        queryResult.setList(list);
        queryResult.setTotal(total);
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }
}
